package com.ygj.purchaseOrderApproval.action;


import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.cjy.common.bean.OpAttributes;
import com.ygj.documentProcessing.action.DocumentProcessingAction;
import com.ygj.purchaseOrderApproval.service.PurchaseOrderApprovalService;

@Action("/purchaseOrderApprovalAction")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class PurchaseOrderApprovalAction extends AbstractBaseAction{
	
	private static final long serialVersionUID = -5330671492474804433L;

	private static final Logger log = LoggerFactory
			.getLogger(DocumentProcessingAction.class);

	@Resource
	private PurchaseOrderApprovalService purchaseOrderApprovalService;
	
	private String username;
	
	private int page;
	
	private String skey;
	
	private int isLastflag;
	
	private String ps;
	
	private int sn;
	
	private String csgmListStr;
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getSkey() {
		return skey;
	}

	public void setSkey(String skey) {
		this.skey = skey;
	}

	public int getIsLastflag() {
		return isLastflag;
	}

	public void setIsLastflag(int isLastflag) {
		this.isLastflag = isLastflag;
	}

	public String getPs() {
		return ps;
	}

	public void setPs(String ps) {
		this.ps = ps;
	}

	public int getSn() {
		return sn;
	}

	public void setSn(int sn) {
		this.sn = sn;
	}

	public String getCsgmListStr() {
		return csgmListStr;
	}

	public void setCsgmListStr(String csgmListStr) {
		this.csgmListStr = csgmListStr;
	}

	@Action("/queryPurchaseOrderApprovalCounts")
	public String queryPurchaseOrderApprovalCounts(){
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", 1).add("result", new OpAttributes().add("totalCounts", purchaseOrderApprovalService.queryPurchaseOrderApprovalTotalCounts(username))));
		} catch (Exception e) {
			e.printStackTrace();
			log.error("purchaseOrderApprovalAction queryPurchaseOrderApprovalCounts error", e);
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		
		return returnType();
	}
	
	@Action("/queryPurchaseOrderApproval")
	public String queryPurchaseOrderApproval(){
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", 1).add("result", purchaseOrderApprovalService.queryPurchaseOrderApproval(username, page)));
		} catch (Exception e) {
			e.printStackTrace();
			log.error("purchaseOrderApprovalAction queryPurchaseOrderApproval error", e);
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		
		return returnType();
	}
	
	@Action("/queryPurchaseOrderApprovalDetail")
	public String queryPurchaseOrderApprovalDetail(){
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", 1).add("result", purchaseOrderApprovalService.queryPurchaseOrderApprovalDetail(skey)));
		} catch (Exception e) {
			e.printStackTrace();
			log.error("purchaseOrderApprovalAction queryPurchaseOrderApprovalDetail error", e);
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		
		return returnType();
	} 
	
	@Action("/approve")
	public String approve() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", 1).add("result", purchaseOrderApprovalService.approve(skey, isLastflag, ps, sn, csgmListStr)));
		} catch (Exception e) {
			e.printStackTrace();
			log.error("purchaseOrderApprovalAction approve error", e);
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		
		return returnType();
	}
	
	@Action("/purchaseOrderBackToTheDrafters")
	public String backToTheDrafters() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", purchaseOrderApprovalService.backToTheDrafters(skey, sn, ps)));
		} catch (Exception e) {
			log.error("purchaseOrderApprovalAction backToTheDrafters error", e);
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		
		return returnType();
	}
	
	@Action("/purchaseOrderCancelTheOrderToLast")
	public String cancelTheOrderToLast() {
		try {
			actionResult = new ActionResult(new OpAttributes().add("code", purchaseOrderApprovalService.cancelTheOrderToLast(skey, sn, ps)));
		} catch (Exception e) {
			log.error("purchaseOrderApprovalAction cancelTheOrderToLast error", e);
			actionResult = new ActionResult(new OpAttributes().add("code", 0));
		}
		
		return returnType();
	}
}
